' Written by Craig'n'Dave
Module Module1
    ' Depth first traversal using a graph
    Function dft(graph As Dictionary(Of String, List(Of String)), current_vertex As String)
        Dim visited As New List(Of String)
        Dim edges As List(Of String)
        Dim s As New Stack
        Do While current_vertex <> Nothing
            edges = graph.Item(current_vertex)
            edges.Reverse()
            If Not visited.Contains(current_vertex) Then
                visited.Add(current_vertex)
            End If
            For Each vertex In edges
                If Not visited.Contains(vertex) Then
                    s.Push(vertex)
                End If
            Next
            If s.Count > 0 Then current_vertex = s.Pop() Else current_vertex = Nothing
        Loop
        Return visited
    End Function

    Sub main()
        ' Main program starts here
        Dim graph = New Dictionary(Of String, List(Of String)) From {
            {"A", New List(Of String) From {"B", "C", "D"}},
            {"B", New List(Of String) From {"E", "A"}},
            {"C", New List(Of String) From {"A", "D"}},
            {"D", New List(Of String) From {"A", "C", "F"}},
            {"E", New List(Of String) From {"B", "G"}},
            {"F", New List(Of String) From {"D"}},
            {"G", New List(Of String) From {"E"}}
            }
        Dim visited As New List(Of String)
        visited = dft(graph, "A")
        Console.WriteLine(String.Join(", ", visited))
    End Sub
End Module
